    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package braipetro.client.regras;

import java.util.Stack;

/**
 *
 * @author kenreurison
 */
public class Interpretador {

    private String inFixCmd;
    private String posFix = "";

    public Interpretador(String inFixCmd) {
        this.inFixCmd = inFixCmd;
    }

    public String getPostFixCmd() {
        return posFix;
    }

    public void setPostFixCmd(String postFixCmd) {
        this.posFix = postFixCmd;
    }

    public String getInFixCmd() {
        return inFixCmd;
    }

    public void setInFixCmd(String inFixCmd) {
        this.inFixCmd = inFixCmd;
    }

    private int priority(String op) {

        int p = 0;
        switch (op) {
            case "(":
                p = 1;
                break;

            case "|":
                p = 2;
                break;

            case "&":
                p = 3;
        }

        return (p);
    }

    public static boolean isOperator(String op) // verifica se o caractere é operador
    {
        return ("(".equals(op) || ")".equals(op) || "&".equals(op) || "|".equals(op));
    }
    /*   
     public void infixaToPosfixa() {
        
     //String[] splitC = inFixCmd.split(" ");
        
     Stack P;
     P = new Stack();
     char topo;

     char caracterelido = inFixCmd.charAt(0);
        
     int i=1;
        
     while (caracterelido != '.') {
     if (!isOperator(caracterelido)) {
     posFix = posFix + caracterelido;
     } else if (P.isEmpty() || caracterelido == '('){
     P.push(caracterelido);
     } else if (caracterelido == ')') {
     topo = (char) P.peek();
     while (topo != '(') {
     posFix = posFix + topo;
     P.pop();
     topo = (char)P.peek();

     }
     P.pop();
     } else {
     topo = (char)P.peek();
     while (!P.isEmpty() && priority(topo) >= priority(caracterelido)) {
     posFix = posFix + topo;
     P.pop();
     if (!P.isEmpty()) {
     topo = (char)P.peek();
     }
     }
     P.push(caracterelido);
     }
     caracterelido = inFixCmd.charAt(i++);
     }

     while (!P.isEmpty()) {            // últimos operadores que sobraram
     posFix = posFix + P.peek();
     P.pop();
     }
     //posFix = posFix + "";
     }
     */

    public void infixaToPosfixa2() {

        identar();
        if(!isValid()){
            posFix = "";
            return;
        }
        String[] sInfCmd = inFixCmd.split(" ");

        String separator = " ";
        Stack P;
        P = new Stack();
        String topo;

        String termoLido = sInfCmd[0];

        int i = 1;

        while (!".".equals(termoLido)) {
            if (!isOperator(termoLido)) {
                posFix = posFix + termoLido + separator;
            } else if (P.isEmpty() || "(".equals(termoLido)) {
                P.push(termoLido);
            } else if (")".equals(termoLido)) {
                topo = (String) P.peek();
                while (!"(".equals(topo)) {
                    posFix = posFix + topo + separator;
                    P.pop();
                    topo = (String) P.peek();

                }
                P.pop();
            } else {
                topo = (String) P.peek();
                while (!P.isEmpty() && priority(topo) >= priority(termoLido)) {
                    posFix = posFix + topo + separator;
                    P.pop();
                    if (!P.isEmpty()) {
                        topo = (String) P.peek();
                    }
                }
                P.push(termoLido);
            }
            termoLido = sInfCmd[i++];
        }

        while (!P.isEmpty()) {
            posFix = posFix + P.peek() + separator;
            P.pop();
        }
        posFix = posFix + "";
    }

    private void identar() {
        while (inFixCmd.contains("  ")) {
            inFixCmd = inFixCmd.replace("  ", " ");
        }
    }

    private boolean isValid() {
        String[] split = inFixCmd.split(" ");
        for (String s : split) {
            if (isDigit(s)) {
                return false;
            }
        }
        return true;
    }

    private boolean isDigit(String s) {
        char ch = s.charAt(0);
        return (ch >= 48 && ch <= 57);
    }

    public static void main(String args[]) {
        Interpretador interpretador = new Interpretador("( R1       | B      )    | C2 & ( D | B ) .");
        interpretador.infixaToPosfixa2();
        System.out.println(interpretador.getPostFixCmd());
    }

}
